package com.njit.farm.service.impl;

import com.njit.farm.dao.FOpreationMapper;
import com.njit.farm.dataobject.FOpreation;
import com.njit.farm.model.Opreation;
import com.njit.farm.model.Result;
import com.njit.farm.service.OpreationService;
import com.njit.farm.util.IDUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OpreationServiceImpl implements OpreationService {

    @Resource
    FOpreationMapper fOpreationMapper;


    @Override
    public Result add(String lid, Opreation opreation) {

        Result result = new Result();

        FOpreation fOpreation = new FOpreation();
        fOpreation.setlId(lid);
        fOpreation.setoId(IDUtils.uuid());
        fOpreation.setoCost(opreation.getCost());
        fOpreation.setoState(opreation.getState());
        fOpreation.setoType(opreation.getType());

        fOpreationMapper.insert(fOpreation);

        result.setSucc(true);
        return result;
    }

    @Override
    public Result queryByLandId(String lid) {

        Result result = new Result();

        List<FOpreation> fOpreations = fOpreationMapper.selectByLandId(lid);
        List<Opreation> opreationList = new ArrayList<>();


        for (FOpreation fOpreation : fOpreations) {
            opreationList.add(fOpreation.toModel());
        }

        result.setSucc(true);
        result.setData(opreationList);
        return result;
    }

    @Override
    public Result update(Opreation opreation) {

        Result result = new Result();

        FOpreation fOpreation = new FOpreation();
        fOpreation.setoId(opreation.getId());
        fOpreation.setoType(opreation.getType());
        fOpreation.setoState(opreation.getState());
        fOpreation.setoCost(opreation.getCost());

        fOpreationMapper.updateByPrimaryKey(fOpreation);

        result.setSucc(true);
        return result;
    }
}
